<template>
  <!--导师申报课题-->
  <div class="tutor-declare">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" :label-args="{name: this.actionType === 'VIEW_TUTOR' ? $t('graduationDesign.view'): $t('graduationDesign.declare')}" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <ConditionCard label-width="100px" label-position="right" @search="searchList" @reset="reset" :expandable="false" :expand="false">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker clearable v-model="queryForm.grade" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.defaultCurrentYear')">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </ConditionCard>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.tutorDeclareSubject')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <p class="tutor-declare-tips">{{$t('graduationDesign.tutorDeclareSubjectListTipsA') + subjectPlanNum + $t('graduationDesign.tutorDeclareSubjectListTipsB')}}</p>
          </div>
          <div class="float-right">
            <el-button type="danger" @click="onDelete($event, 'BATCH')" v-premit="delete_subject">{{$t('graduationDesign.delete')}}</el-button>
            <el-button type="primary" @click="onDeclare" v-premit="declare_subject">{{$t('graduationDesign.declareSubject')}}</el-button>
          </div>
        </div>
        <edu-table ref="tutorDeclareTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectSource')" prop="projectSource" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectNature')" prop="projectNature" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.applicableCollege')" prop="collegeI18n" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.applicableMajor')" prop="subjectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.uploadTime')" prop="submitTime" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.status')" prop="status" min-width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 'WaitCommit'" type="info">{{$t('graduationDesign.draft')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Process'" type="info">{{$t('graduationDesign.pendingReview')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'Reject'" type="danger">{{$t('graduationDesign.rejected')}}</el-tag>
              <el-tag v-else-if="scope.row.status === 'End'" type="success">{{$t('graduationDesign.passed')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="140">
            <template slot-scope="scope">
              <el-button type="text" @click="onEdit(scope.row)" v-if="scope.row.status === 'WaitCommit' || scope.row.status === 'Reject'" v-premit="declare_subject">{{$t('graduationDesign.edit')}}</el-button>
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
              <el-button type="text" @click="onDelete(scope.row, 'SINGLE')" v-if="scope.row.status === 'WaitCommit' || scope.row.status === 'Reject'" v-premit="delete_subject">{{$t('graduationDesign.delete')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'DECLARE_TOPIC'">
      <TopicInfo ref="topicInfo" :actionType="actionType" :basicInfoForm="declareTopicForm" :progressData="progressData" @topic-submit="declareSubmit" @topic-save="declareSave" @topic-cancel="declareCancel"></TopicInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/tutorDeclareSubject";
import mixin from 'common/src/mixins/index';

export default {
  name: 'tutorDeclareSubject',
  mixins: [mixin],
  data() {
    return {
      getList: '/api/designservice/optTeacher/list',
      api: API,
      queryForm: {
        grade: ''
      },
      declareTopicForm: {},
      indexKey: 'MAIN',
      breadPath: {
        label: this.$t('graduationDesign.tutorDeclareSubject'),
        key: "MAIN",
        children: [
          {
            key: "DECLARE_TOPIC",
            label: "{name}" + this.$t('graduationDesign.subject')
          }
        ]
      },
      tutorSelectedData: [],
      subjectPlanNum: 0, // 课题计划数
      actionType: '',
      userInfo: {}, // 当前用户信息
      progressData: [] //审核进度列表
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + "";
    this.searchList();
    this.userInfo = { ...this.global };
  },
  methods: {
    async searchList() {
      await this.$refs.tutorDeclareTable.search(this.queryForm);
      this.subjectPlanNum = this.$refs.tutorDeclareTable.getTotal();
    },
    reset() {
      this.queryForm = {
        grade: new Date().getFullYear() + ""
      };
      this.searchList();
    },
    handleSelectChange(val) {
      this.tutorSelectedData = val;
    },
    // 申报课题
    onDeclare(row) {
      this.indexKey = "DECLARE_TOPIC";
      this.declareTopicForm = {
        projectName: '',
        subtitleName: '',
        researchDirection: '',
        introductionProject: '',
        college: '',
        subjectId: '',
        direction: '',
        isOutside: false,
        outsideTeacherNum: '',
        projectType: '',
        projectSourceId: '',
        projectNatureId: '',
        practiceSocialId: '',
        designPlace: '',
        teacherNum: ''
      };
      this.actionType = "ADD";
      this.declareTopicForm.college = this.userInfo.currentUser.faculty;
    },
    // 编辑
    onEdit(row) {
      this.indexKey = "DECLARE_TOPIC";
      this.declareTopicForm = { ...row };
      this.declareTopicForm.grade = row.grade ? row.grade + '' : '';
      this.actionType = "EDIT";
    },
    // 查看
    onView(row) {
      this.indexKey = "DECLARE_TOPIC";
      this.declareTopicForm = { ...row };
      this.declareTopicForm.grade = row.grade ? row.grade + '' : '';
      this.actionType = "VIEW_TUTOR";
      this.progressData = [];
      if (row.flowNo) {
        this.getReviewProgress(row.flowNo);
      }
    },
    // 查审核进度
    getReviewProgress(applyNo) {
      this.api.getProgress(applyNo).then(res => {
        this.progressData = res.data.listApproveHistory;
      }).catch();
    },
    // 删除
    onDelete(row, type) {
      let params = [];
      if (type === 'BATCH') {
        if (!this.tutorSelectedData.length) {
          this.$message.warning(this.$t('graduationDesign.pleaseSelectData'));
          return;
        }
        params = this.tutorSelectedData;
      } else {
        params = [{ ...row }];
      }
      this.$delConfirm(this.$t('graduationDesign.deleteConfirm'), this.$t('graduationDesign.delete')).then(() => {
        this.api.delete(params).then(res => {
          this.$message.success(this.$t('graduationDesign.deleteSuccess'));
          this.searchList();
        }).catch();
      });
    },
    // 保存
    declareSave(params) {
      this.$refs.topicInfo.$refs["basicInfoForm"].validate(valid => {
        if (valid) {
          let promise = null;
          if (params.actionType === "EDIT") {
            promise = this.api.update(params.data);
          } else {
            promise = this.api.save(params.data);
          }
          promise.then(res => {
            this.$message.success(this.$t('graduationDesign.saveSuccess'));
            this.indexKey = "MAIN";
            this.searchList();
          }).catch();
        } else {
          return false;
        }
      });
    },
    // 提交
    declareSubmit(data) {
      this.$refs.topicInfo.$refs["basicInfoForm"].validate(valid => {
        if (valid) {
          this.api.submit(data).then(res => {
            this.$message.success(this.$t('graduationDesign.submitSuccess'));
            this.indexKey = "MAIN";
            this.searchList();
          }).catch();
        } else {
          return false;
        }
      });
    },
    // 取消
    declareCancel() {
      this.indexKey = "MAIN";
      this.searchList();
    }
  },
  components: {
    TopicInfo: () => import('../components/topicInfo')
  }
}
</script>
<style lang="scss" scoped>
.tutor-declare {
    .dateclass {
        width: 100%;
    }
    .tutor-declare-tips {
        color: #67c23a;
    }
}
</style>
